﻿using OrderStateMachineWebApi.Models;
using OrderStateMachineWebApi.Services;

namespace OrderStateMachineWebApi.ServicesImpl
{

    public class OrderStateMachineService : IOrderStateMachineService
    {
        private readonly IOrderRepository _orderRepository;
        private readonly OrderStateFactory _stateFactory;

        public OrderStateMachineService(IOrderRepository orderRepository)
        {
            _orderRepository = orderRepository;
            _stateFactory = new OrderStateFactory();
        }

        public async Task<Order> CreateOrderAsync(decimal amount)
        {
            return await _orderRepository.CreateOrderAsync(amount);
        }

        public async Task<Order> GetOrderByIdAsync(Guid id)
        {
            return await _orderRepository.GetOrderByIdAsync(id);
        }

        public async Task<Order> PayOrderAsync(Guid orderId)
        {
            var order = await GetOrderByIdAsync(orderId)
                ?? throw new KeyNotFoundException("订单不存在");

            var state = _stateFactory.GetState(order.Status);
            await state.PayAsync(order);

            await _orderRepository.UpdateOrderAsync(order);
            return order;
        }

        public async Task<Order> ProcessOrderAsync(Guid orderId)
        {
            var order = await GetOrderByIdAsync(orderId)
                ?? throw new KeyNotFoundException("订单不存在");

            var state = _stateFactory.GetState(order.Status);
            await state.ProcessAsync(order);

            await _orderRepository.UpdateOrderAsync(order);
            return order;
        }

        public async Task<Order> ShipOrderAsync(Guid orderId)
        {
            var order = await GetOrderByIdAsync(orderId)
                ?? throw new KeyNotFoundException("订单不存在");

            var state = _stateFactory.GetState(order.Status);
            await state.ShipAsync(order);

            await _orderRepository.UpdateOrderAsync(order);
            return order;
        }

        public async Task<Order> CompleteOrderAsync(Guid orderId)
        {
            var order = await GetOrderByIdAsync(orderId)
                ?? throw new KeyNotFoundException("订单不存在");

            var state = _stateFactory.GetState(order.Status);
            await state.CompleteAsync(order);

            await _orderRepository.UpdateOrderAsync(order);
            return order;
        }

        public async Task<Order> CancelOrderAsync(Guid orderId)
        {
            var order = await GetOrderByIdAsync(orderId)
                ?? throw new KeyNotFoundException("订单不存在");

            var state = _stateFactory.GetState(order.Status);
            await state.CancelAsync(order);

            await _orderRepository.UpdateOrderAsync(order);
            return order;
        }

        public async Task<Order> StartRefundOrderAsync(Guid orderId)
        {
            var order = await GetOrderByIdAsync(orderId)
                ?? throw new KeyNotFoundException("订单不存在");

            var state = _stateFactory.GetState(order.Status);
            await state.StartRefundAsync(order);

            await _orderRepository.UpdateOrderAsync(order);
            return order;
        }

        public async Task<Order> CompleteRefundOrderAsync(Guid orderId)
        {
            var order = await GetOrderByIdAsync(orderId)
                ?? throw new KeyNotFoundException("订单不存在");

            var state = _stateFactory.GetState(order.Status);
            await state.CompleteRefundAsync(order);

            await _orderRepository.UpdateOrderAsync(order);
            return order;
        }
    }

}
